﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class DataClearController : ControllerBase
    {
        IHttpContextAccessor _access;
        public DataClearController(IHttpContextAccessor access
          )
        {   
            _access = access;
        }

        /// <summary>
        /// 查询所有表
        /// </summary>
        [HttpPost]
        [Route("QueryAllTable")]
        public ResponseMessage QueryAllTable()
        {
            ResponseMessage message = new ResponseMessage();
            DataClearBUS BUS = new DataClearBUS();
            try
            {
                message = BUS.QueryAllTable();
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询所有表";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询所有表异常";
            }
            return message;
        }

        /// <summary>
        /// 清理数据
        /// </summary>
        [HttpPost]
        [Route("ClearTable")]
        public ResponseMessage ClearTable(string tablename)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearBUS BUS = new DataClearBUS();
            try
            {
                message = BUS.ClearTable(tablename);
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "清空数据";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "清空数据异常";
            }
            return message;
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        [HttpPost]
        [Route("QueryErrorLog")]
        public ResponseMessage QueryErrorLog(ErrorLog errorLog)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearBUS BUS = new DataClearBUS();
            try
            {
                message = BUS.QueryErrorLog(errorLog);
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog1 = new ErrorLog();
                errorLog1.ErrorMsg = e.ToString();
                errorLog1.Operation = "查询系统日志";
                dataClearBUS.AddErrorLog(errorLog1);
                message.errcode = 1;
                message.errmsg = "查询系统日志异常";
            }
            return message;
        }
    }
}
